//
//  CouponMovieDetailViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-12.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "CouponMovieDetailViewController.h"
#import "BidResultViewController.h"

@implementation CouponMovieDetailViewController

@synthesize movieNameLabel;
@synthesize movieIntroLabel;
@synthesize movieOffPriceLabel;
@synthesize movieFullPosterView;
@synthesize movieTimeLabel;
@synthesize cinemaNameLabel;
@synthesize cinemaAddressLabel;
@synthesize ticketsNumButton;
@synthesize ticketsLeft;
@synthesize phoneButton;
@synthesize mapButton;
@synthesize couponMovieRecord;
@synthesize movieOriginPriceLabel;


- (id)initWithMovieRecord:(MovieRecord *)aRecord
{
    self = [super init];
    if (self) {
        self.couponMovieRecord = (CouponMovieRecord *)aRecord;
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
        self.optionTable.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [movieNameLabel release];
    [movieTimeLabel release];
    [cinemaNameLabel release];
    [cinemaAddressLabel release];
    [movieIntroLabel release];
    [ticketsNumButton release];
    [phoneButton release];
    [mapButton release];
    [movieOffPriceLabel release];
    [movieFullPosterView release];
    [ticketsLeft release];
    [movieOriginPriceLabel release];
    
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)configureButtonTitle
{
    [self.ticketsNumButton setTitle:tickesWant_ forState:UIControlStateNormal];
}

- (void)configure
{
    tickesWant_ = @"1张";
    
    // Set the movie name label
    self.movieNameLabel.text = [self.couponMovieRecord movieName];
    
    // Set the movie time and cinema info
    self.movieTimeLabel.text = [self.couponMovieRecord movieTime];
    self.cinemaNameLabel.text = [self.couponMovieRecord cinemaName];
    self.cinemaAddressLabel.text = [self.couponMovieRecord cinemaAddress];
    
    // Set the movie intro info
    self.movieIntroLabel.text = [self.couponMovieRecord movieIntro];
    self.movieFullPosterView.image = [self.couponMovieRecord movieFullPoster];
    
    // Set the movie price info
    self.ticketsLeft.text = [NSString stringWithFormat:@"还剩%d张", self.couponMovieRecord.ticketsNum];
    self.movieOffPriceLabel.text = [self priceLabelwithPrice:self.couponMovieRecord.movieOffPrice];
    
    // Configure the scroll view 
    self.scrollView.contentSize = CGSizeMake(420.0f, 369.0f);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.scrollView setContentOffset:CGPointMake(70.0f, 0) animated:YES];
    
    // Configure the table view
}

- (void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    // Load the cell nib
    self.cellNib = [UINib nibWithNibName:@"DetailTableViewCell" bundle:nil];
    
    // Configure the view
    [self configure];
    
    // Set the nav bar title
    [self addTitleToNavigationBar:@"优惠信息"];
    
    // Set the back bar item 
    [self createBarItemOn:kNavBarItemSideLeft frame:kNavBarLeftItemFrame title:@"返回" target:self action:@selector(popBackToLastViewController)];
    
    // call its super to load
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.movieNameLabel = nil;
    self.movieTimeLabel = nil;
    self.cinemaNameLabel = nil;
    self.cinemaAddressLabel = nil;
    self.movieIntroLabel = nil;
    self.movieFullPosterView = nil;
    self.movieOffPriceLabel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)selectionDone
{
    NSString *changeContent = [self pickerView:self.dataPicker titleForRow:self.wantTicketsNum forComponent:0];
    
    tickesWant_ = changeContent;
    
    NSLog(@"%@", tickesWant_);
    
    [actionSheet_ dismissWithClickedButtonIndex:0 animated:YES];
    
    [self configureButtonTitle];
}

- (IBAction)buttonPressed:(UIButton *)button
{
    NSInteger buttonTag = [button tag];
    
    if (buttonTag == 2001) {
        [self makePhoneCallWithTel:@"13717719950"];
    }
    else if (buttonTag == 2002 ){
        GPSViewController *gpsViewController = [[GPSViewController alloc] init];
        [self.navigationController pushViewController:gpsViewController animated:YES];
    }
    else {
        actionSheet_ = [self actionSheetWithType:PICKER_NUM];
    }
}

- (IBAction)goButtonPressed
{
    BidResultViewController *viewController = [[BidResultViewController alloc] initWithTitle:[self.couponMovieRecord movieName]];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}

@end
